dataviz\figure\datasets/
dataset.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use super::{
    areachartdataset::AreaChartDataset, bardataset::BarDataset,
    cartesiangraphdataset::CartesianDataset, scattergraphdataset::ScatterGraphDataset,
};

/// A trait for managing datasets used in different types of charts or graphs.
pub trait Dataset {
    /// Retrieves all points in the dataset as a vector of `(x, y)` tuples.
    ///
    /// # Returns
    /// A vector of `(f64, f64)` representing the data points in the dataset.
    fn get_points(&self) -> Vec<(f64, f64)>;

    /// Adds a single point to the dataset.
    ///
    /// # Parameters
    /// - `point`: A tuple `(f64, f64)` representing the x and y coordinates of the point to add.
    fn add_point(&mut self, point: (f64, f64));
}

impl Dataset for BarDataset {
    /// Implementation of the `Dataset` trait for `BarDataset`.
    ///
    /// - `get_points`: Returns the bar data as `(x, y)` pairs.
    /// - `add_point`: Adds a new `(x, y)` pair to the bar dataset.
    fn get_points(&self) -> Vec<(f64, f64)> {
        self.data.clone()
    }

    fn add_point(&mut self, point: (f64, f64)) {
        self.data.push(point);
    }
}

impl Dataset for CartesianDataset {
    /// Implementation of the `Dataset` trait for `CartesianDataset`.
    ///
    /// - `get_points`: Returns the Cartesian data as `(x, y)` pairs.
    /// - `add_point`: Adds a new `(x, y)` pair to the Cartesian dataset.
    fn get_points(&self) -> Vec<(f64, f64)> {
        self.points.clone()
    }

    fn add_point(&mut self, point: (f64, f64)) {
        self.points.push(point);
    }
}

impl Dataset for ScatterGraphDataset {
    /// Implementation of the `Dataset` trait for `ScatterGraphDataset`.
    ///
    /// - `get_points`: Returns the scatter graph data as `(x, y)` pairs.
    /// - `add_point`: Adds a new `(x, y)` pair to the scatter graph dataset.
    fn get_points(&self) -> Vec<(f64, f64)> {
        self.points.clone()
    }

    fn add_point(&mut self, point: (f64, f64)) {
        self.points.push(point);
    }
}

impl Dataset for AreaChartDataset {
    /// Implementation of the `Dataset` trait for `AreaChartDataset`.
    ///
    /// - `get_points`: Returns the area chart data as `(x, y)` pairs.
    /// - `add_point`: Adds a new `(x, y)` pair to the area chart dataset.
    fn get_points(&self) -> Vec<(f64, f64)> {
        self.points.clone()
    }

    fn add_point(&mut self, point: (f64, f64)) {
        self.points.push(point);
    }
}